package com.aaa.pro.controller;

import com.aaa.entity.PmsComment;
import com.aaa.pro.service.CommonService;
import com.aaa.pro.service.ProAppraiseService;
import com.aaa.pro.util.OSSBootUtil;
import com.aaa.pro.util.OssProperties;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname ProAppraiseController
 * @Description TODO
 * @Date 2020/1/12 15:56
 * @Created by admin
 * @author:GT
 * @version:1.0.0
 */
@RestController
public class ProAppraiseController {

    @Autowired
    ProAppraiseService proAppraiseService;

    @Autowired
    CommonService commonService;

    @RequestMapping("getProAppraise")
    public Object proAppraise (){
        Map map = new HashMap();
        map.put("getProAppraise",proAppraiseService.getProAppraise());
//        System.out.println(map);
        return map;
    }

    /**
     * 获取首页评论的数据
     * @return
     */
    @RequestMapping("getCommentData")
    public Object getCommentData(){
        Map map = new HashMap();
        map.put("homeComment",proAppraiseService.getHomeComment());
        map.put("commentData",proAppraiseService.getCommentData());
        return map;
    }

    /**
     * 测试---
     * @return
     */
    @RequestMapping("getAllEva")
    public Object getAllEvaluation(int sequenceNum){
        return proAppraiseService.getAllevaluation(sequenceNum);
    }

    /**
     * 上传文件至阿里云 oss
     *
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/upload/oss", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<?> uploadOSS(@RequestParam(value = "file") MultipartFile file) throws Exception {
        Map result = commonService.uploadOSS(file);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        return new ResponseEntity<>(result, headers, HttpStatus.CREATED);
    }

    /**
     * 添加评论方法
     * @param map
     * @return
     */
    @RequestMapping(value = "addCom",method = RequestMethod.POST)
    public int AddComment(@RequestBody Map map){
        if(map.get("pics").toString().length() != 0) {
            map.put("pics", map.get("pics").toString().split("\"")[1]);
        }
        return proAppraiseService.AddComment(map);
    }

}
